﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
//new
using System.Xml;
using System.IO;

namespace JKZP
{
    public partial class SysParamForm : Form
    {
        public SysParamForm()
        {
            InitializeComponent();
        }

        private void SysParamForm_Load(object sender, EventArgs e)
        {
            try
            {
                textBox_SaveFolder.Text = PP.gJKSavePath;
                checkBox_RecordEnable.Checked = PP.gJKRecordEnable;
                textBox_RecordFolder.Text = PP.gJKRecordPath;
                checkBox_jkSaveBySubFolder.Checked = PP.gJKSaveBySubFolder;
                textBox_defaultHPHM.Text = PP.gDefaultHPHM;
                checkBox_JariTailEnable.Checked = PP.gJARITailEnable;
                checkBox_piconly.Checked = PP.gUploadPicOnly;

                checkBox_RecordEnable_CheckedChanged(sender, e);
                checkBox_jkSaveBySubFolder_CheckedChanged(sender, e);
                //绑定卡口文件夹
                this.dataGridView_jkfolder.AutoGenerateColumns = false;
                ((DataGridViewComboBoxColumn)this.dataGridView_jkfolder.Columns["jkfoldertype"]).ValueMember = "jkfoldertype";
                ((DataGridViewComboBoxColumn)this.dataGridView_jkfolder.Columns["jkfoldertype"]).DisplayMember = "jkfoldertype";
                DataTable dt_jkfolder = new DataTable("jkfolder");
                dt_jkfolder.Columns.Add("jkfoldertype");
                foreach (string s in Enum.GetNames(typeof(PP.FOLDERSAVEMODE)))
                {
                    dt_jkfolder.Rows.Add(s);
                }
                ((DataGridViewComboBoxColumn)this.dataGridView_jkfolder.Columns["jkfoldertype"]).DataSource = dt_jkfolder;


                //绑定卡口命名
                this.dataGridView_jkname.AutoGenerateColumns = false;
                ((DataGridViewComboBoxColumn)this.dataGridView_jkname.Columns["jknametype"]).ValueMember = "jknametype";
                ((DataGridViewComboBoxColumn)this.dataGridView_jkname.Columns["jknametype"]).DisplayMember = "jknametype";
                DataTable dt_jkname = new DataTable("jkname");
                dt_jkname.Columns.Add("jknametype");
                foreach (string s in Enum.GetNames(typeof(PP.PICNAMEMODE)))
                {
                    dt_jkname.Rows.Add(s);
                }
                ((DataGridViewComboBoxColumn)this.dataGridView_jkname.Columns["jknametype"]).DataSource = dt_jkname;

                //卡口子文件夹参数
                this.dataGridView_jkfolder.Rows.Clear();
                if (PP.gJKFolderTypeList != null)
                {
                    foreach (string s in PP.gJKFolderTypeList)
                    {
                        this.dataGridView_jkfolder.Rows.Add(new object[2] { s, "删除" });
                    }
                }

                //卡口命名参数
                this.dataGridView_jkname.Rows.Clear();
                if ((PP.gJKNameTypeList != null) && (PP.gJKNameTextList != null))
                {
                    for (int i = 0; i < PP.gJKNameTypeList.Count; i++)
                    {
                        this.dataGridView_jkname.Rows.Add(PP.gJKNameTypeList[i], PP.gJKNameTextList[i], "删除");
                    }
                }
            }
            catch { }
        }

        private void button_jk_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == this.folderBrowserDialog1.ShowDialog())
            {
                this.textBox_SaveFolder.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }

        private void checkBox_jkSaveBySubFolder_CheckedChanged(object sender, EventArgs e)
        {
            this.dataGridView_jkfolder.Visible = this.checkBox_jkSaveBySubFolder.Checked;
        }

        private void dataGridView_jkfolder_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((this.dataGridView_jkfolder.CurrentCell.ColumnIndex == 1) && (!this.dataGridView_jkfolder.CurrentRow.IsNewRow))
            {
                this.dataGridView_jkfolder.Rows.Remove(this.dataGridView_jkfolder.CurrentRow);
            }
        }

        private void dataGridView_jkname_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((this.dataGridView_jkname.CurrentCell.ColumnIndex == 2) && (!this.dataGridView_jkname.CurrentRow.IsNewRow))
            {
                this.dataGridView_jkname.Rows.Remove(this.dataGridView_jkname.CurrentRow);
            }
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            string hphmxpath = "descendant::DefaultHPHM";
            string jkxpath = "descendant::JKSavePath";
            string jkSaveBySubFolderXpath = "descendant::JKSaveBySubFolder";
            string[] jkkey = new string[1] { "enable" };
            string[] jkvalue = new string[1] { this.checkBox_jkSaveBySubFolder.Checked.ToString() };
            string jkSubFolderXpath = "descendant::JKSubFolder";
            string jkPicNameXpath = "descendant::JKPicName";
            string JKRecordEnableXpath = "descendant::JKRecordEnable";
            string JKRecordPathXpath = "descendant::JKRecordPath";
            string JARITailEnableXpath = "descendant::JARITailEnable";
            string UploadPicOnlyXpath = "descendant::UploadPicOnly";
            if ((PP.SetConfig(PP.gConfigFile, JKRecordEnableXpath,checkBox_RecordEnable.Checked.ToString(), null, null) == true) &&
                (PP.SetConfig(PP.gConfigFile, JARITailEnableXpath, checkBox_JariTailEnable.Checked.ToString(), null, null) == true) &&
                (PP.SetConfig(PP.gConfigFile, UploadPicOnlyXpath, checkBox_piconly.Checked.ToString(), null, null) == true) &&
                (PP.SetConfig(PP.gConfigFile, JKRecordPathXpath, this.textBox_RecordFolder.Text, null, null) == true) &&
                (PP.SetConfig(PP.gConfigFile, jkxpath, textBox_SaveFolder.Text, null, null) == true) &&
                (PP.SetConfig(PP.gConfigFile, hphmxpath, textBox_defaultHPHM.Text, null, null) == true) &&
                (PP.SetConfig(PP.gConfigFile, jkSaveBySubFolderXpath, "", jkkey, jkvalue) == true) &&
                (PP.DelConfig(PP.gConfigFile, jkSubFolderXpath, false) == true) &&
                (PP.DelConfig(PP.gConfigFile, jkPicNameXpath, true) == true) &&
                (SaveFolderAndName() == true)
                )
            {
                PP.gDefaultHPHM = textBox_defaultHPHM.Text;
                PP.gJKSavePath = textBox_SaveFolder.Text;
                PP.gJKSaveBySubFolder = this.checkBox_jkSaveBySubFolder.Checked;
                PP.gJARITailEnable = this.checkBox_JariTailEnable.Checked;
                PP.gUploadPicOnly = this.checkBox_piconly.Checked;
                PP.gJKRecordEnable = this.checkBox_RecordEnable.Checked;
                PP.gJKRecordPath = this.textBox_RecordFolder.Text;
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(PP.gConfigFile);
                    PP.gJKFolderTypeList = PP.ReadAllAttributeName(doc, "JKSubFolder");
                    PP.gJKNameTypeList = PP.ReadAllSubNodeAttribute0Names(doc, "JKPicName");
                    PP.gJKNameTextList = PP.ReadAllSubNodeAttribute0Values(doc, "JKPicName");
                }
                catch { }

                if (PP.gISWebVersion == true)
                {
                    if (PP.kdws.JKZP_UploadParam(File.ReadAllText(PP.gConfigFile)) == true)
                    {
                        toolStripStatusLabel1.Text = "保存成功";
                    }
                    else
                    {
                        toolStripStatusLabel1.Text = "保存失败";
                    }
                }
                else
                {
                    toolStripStatusLabel1.Text = "保存成功";
                }
            }
        }

        private bool SaveFolderAndName()
        {
            bool ans = true;
            bool flag1 = true;
            bool flag2 = true;
            //string kkSubFolderXpath = "descendant::KKSubFolder";
            //string djSubFolderXpath = "descendant::DJSubFolder";
            string jkPicNameXpath = "descendant::JKPicName";
            string rootXpath = "descendant::param";
            try
            {
                //卡口文件夹
                string[] s = new string[this.dataGridView_jkfolder.Rows.Count - 1];
                string[] ss = new string[this.dataGridView_jkfolder.Rows.Count - 1];
                for (int i = 0; i < this.dataGridView_jkfolder.Rows.Count - 1; i++)
                {
                    s[i] = this.dataGridView_jkfolder.Rows[i].Cells[0].Value.ToString();
                    ss[i] = "";
                }
                flag1 = PP.AddConfig(PP.gConfigFile, rootXpath, "JKSubFolder", "", s, ss);

                //卡口命名
                s = new string[1];
                ss = new string[1];
                for (int i = 0; i < this.dataGridView_jkname.Rows.Count - 1; i++)
                {
                    if (this.dataGridView_jkname.Rows[i].Cells[0].Value == null)
                    {
                        s[0] = "";
                    }
                    else
                    {
                        s[0] = this.dataGridView_jkname.Rows[i].Cells[0].Value.ToString();
                    }
                    if (this.dataGridView_jkname.Rows[i].Cells[1].Value == null)
                    {
                        ss[0] = "";
                    }
                    else
                    {
                        ss[0] = this.dataGridView_jkname.Rows[i].Cells[1].Value.ToString();
                    }
                    if (PP.AddConfig(PP.gConfigFile, jkPicNameXpath, "NameMode", "", s, ss) == false)
                    {
                        flag2 = false;
                    }
                }

                ans = (flag1 && flag2);
            }
            catch
            {
                ans = false;
            }
            return ans;
        }

        private void button_RecordFolder_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == this.folderBrowserDialog1.ShowDialog())
            {
                this.textBox_RecordFolder.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }

        private void checkBox_RecordEnable_CheckedChanged(object sender, EventArgs e)
        {
            this.label_record.Visible = this.checkBox_RecordEnable.Checked;
            this.textBox_RecordFolder.Visible = this.checkBox_RecordEnable.Checked;
            this.button_RecordFolder.Visible = this.checkBox_RecordEnable.Checked;
        }
    }
}
